<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');

  class PersonController extends JController
  {
    var $_name = 'person';
  
    function display()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView('person', $document->getType());
      if ($model = & $this->getModel('person')) 
        $view->setModel($model, true);
      $view->displayList();
    }
    
    function edit()
    {
    /*  $cids = JRequest::getVar('cid', null, 'default', 'array' );
      
      if($cids === null)
          JError::raiseError(500, 'cid parameter missing from the request');
    */  
      $document =& JFactory::getDocument();
    
      $view = & $this->getView('person', $document->getType());
      $view->setModel($this->getModel(), true);
      $view->displayEdit();
    }
    
    function save()
    {
      $data = JRequest::get('POST');
      
      if($data['id'])
        $data['updated_on'] = date('Y-m-d',time());
      else
        $data['created_on'] = date('Y-m-d',time());
      
      $person   = $this->getModel('person','personmodel');
      $person->save($data);
      
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, 'Person Saved!');             
    }
    
    function add()
    {
      $document =& JFactory::getDocument();
      $view = & $this->getView('person', $document->getType());
      $view->setModel($this->getModel());
      $view->displayAdd();  
    }
    
    function remove()
    {
             $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
         
             if($ids === null)
       { 
         JError::raiseError(500, 'cid parameter missing from the request');
             }
         
             $model = & $this->getModel($this->_name);
             $model->delete($ids);
         
             $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
             $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Deleted!');                
        }    
    
    function publish()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
       
      $model = & $this->getModel($this->_name); 
      $model->publish($ids, 1);
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Published!');   
    }
    
    function unpublish()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
       
      $model = & $this->getModel($this->_name); 
      $model->publish($ids, 0);
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Unpublished!');       
    }
  }
?>